package com.vision.sales.debitmemo.cl.u;

import static com.vision.sales.cl.SalesServices.companySvc;
import static com.vision.sales.cl.SalesServices.customerSvc;
import static com.vision.sales.cl.SalesServices.debitMemoSvc;

import java.util.ArrayList;
import java.util.List;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.Editor;
import com.google.gwt.editor.client.SimpleBeanEditorDriver;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.core.client.Style.SelectionMode;
import com.sencha.gxt.data.client.loader.RpcProxy;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.Store;
import com.sencha.gxt.data.shared.loader.LoadEvent;
import com.sencha.gxt.data.shared.loader.LoadResultListStoreBinding;
import com.sencha.gxt.data.shared.loader.PagingLoadConfig;
import com.sencha.gxt.data.shared.loader.PagingLoadResult;
import com.sencha.gxt.data.shared.loader.PagingLoader;
import com.sencha.gxt.widget.core.client.TabItemConfig;
import com.sencha.gxt.widget.core.client.Window;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.event.HideEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.form.DateField;
import com.sencha.gxt.widget.core.client.form.NumberField;
import com.sencha.gxt.widget.core.client.form.NumberPropertyEditor;
import com.sencha.gxt.widget.core.client.form.SimpleComboBox;
import com.sencha.gxt.widget.core.client.form.StoreFilterField;
import com.sencha.gxt.widget.core.client.form.TextArea;
import com.sencha.gxt.widget.core.client.form.TextField;
import com.sencha.gxt.widget.core.client.form.validator.MaxLengthValidator;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;
import com.sencha.gxt.widget.core.client.grid.ColumnModel;
import com.sencha.gxt.widget.core.client.grid.Grid;
import com.sencha.gxt.widget.core.client.grid.GridView;
import com.sencha.gxt.widget.core.client.menu.CheckMenuItem;
import com.sencha.gxt.widget.core.client.menu.Item;
import com.sencha.gxt.widget.core.client.menu.MenuItem;
import com.sencha.gxt.widget.core.client.selection.SelectionChangedEvent;
import com.sencha.gxt.widget.core.client.selection.SelectionChangedEvent.SelectionChangedHandler;
import com.sencha.gxt.widget.core.client.toolbar.PagingToolBar;
import com.vision.core.cl.BaseModule;
import com.vision.core.cl.Callback;
import com.vision.core.cl.event.CreatedEvent;
import com.vision.core.cl.event.CreatedEvent.CreationHandler;
import com.vision.core.cl.ui.ConfirmDialogHandler;
import com.vision.core.cl.ui.Notifications;
import com.vision.core.cl.ui.TabItemPanel;
import com.vision.core.cl.ui.UI;
import com.vision.core.cl.ui.component.BasicGridView;
import com.vision.core.cl.ui.component.CompanyComboBox;
import com.vision.core.cl.ui.component.EnumComboBox;
import com.vision.core.cm.db.data.Company;
import com.vision.sales.cl.SalesServices;
import com.vision.sales.cl.SalesTasks;
import com.vision.sales.cl.data.DebitMemoProperties;
import com.vision.sales.cl.event.DebitMemoCreatedEvent;
import com.vision.sales.cl.resource.SalesResources;
import com.vision.sales.cl.ui.component.CustomerComboBox;
import com.vision.sales.cl.ui.component.SalesInvoiceComboBox;
import com.vision.sales.cl.ui.component.TransactionLoadConfig;
import com.vision.sales.cl.ui.component.TransactionNoCell;
import com.vision.sales.cm.data.DataUtils;
import com.vision.sales.cm.data.DebitMemoType;
import com.vision.sales.cm.data.TransactionStatus;
import com.vision.sales.cm.db.data.Customer;
import com.vision.sales.cm.db.data.DebitMemo;
import com.vision.sales.cm.db.data.DebitMemo.FieldSpecs;

/**
 * 
 * @author Mark
 *
 */
public class DebitMemosPanel extends TabItemPanel implements Editor<DebitMemo> {

	interface Binder extends UiBinder<Widget, DebitMemosPanel> { }

	private static final Binder binder = GWT.create(Binder.class);
	
	@UiField VerticalLayoutContainer mainPanel;
	
	// Ignored by the form driver since these are not fields in the DebitMemo
	@Ignore @UiField StoreFilterField<DebitMemo> gridFilter;
	@Ignore @UiField TextButton filterBtn;
	@Ignore @UiField MenuItem statusMenuItem;
		@Ignore @UiField CheckMenuItem pendingOnlyMenuItem;
		@Ignore @UiField CheckMenuItem postedOnlyMenuItem;
		@Ignore @UiField CheckMenuItem allStatusMenuItem;
		
	@Ignore @UiField TextButton addBtn;
	@Ignore @UiField TextButton postBtn;
	@Ignore @UiField TextButton moreBtn;
	@Ignore @UiField MenuItem refreshMenuItem;
	@Ignore @UiField MenuItem deleteMenuItem;
	
	@UiField Grid<DebitMemo> grid;
	@UiField PagingToolBar pagingToolbar;
	PagingLoader<PagingLoadConfig, PagingLoadResult<DebitMemo>> loader;
	
	// Form fields that are bound by the form driver to the fields in the DebitMemo class.
	// The name of these form fields must match the name of the field in the DebitMemo class.
	@UiField CompanyComboBox company;
	@UiField CustomerComboBox customer;
	@UiField TextField debitMemoNo;
	@UiField DateField date;
	@UiField SimpleComboBox<DebitMemoType> type;
	@UiField NumberField<Double> amount;
	@UiField SalesInvoiceComboBox salesInvoice;
	@UiField TextArea remarks;
	
	// Ignored by the form driver since these are not fields in the DebitMemo
	@Ignore @UiField TextButton saveBtn;
	@Ignore @UiField TextButton cancelBtn;
	
	@UiField(provided = true) NumberFormat numberFormat = UI.amountFormat();
	@UiField(provided = true) NumberPropertyEditor<Double> doublePropertyEditor = UI.doubleEditor();
	
	// Interface used to bind the form fields to the DebitMemo fields
	interface DebitMemoDriver extends SimpleBeanEditorDriver<DebitMemo, DebitMemosPanel> { }
	
	// Instance of the DebitMemoDriver interface
	private DebitMemoDriver driver = GWT.create(DebitMemoDriver.class);
	
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	public DebitMemosPanel() {
		// Global Handlers -- should be registered only once
		BaseModule.addHandler(DebitMemoCreatedEvent.TYPE, new CreationHandler<DebitMemo>() {
			@Override
			public void onCreated(CreatedEvent<DebitMemo> e) {
				grid.getStore().add(e.getObject());
			}
		});
		
		setWidget(binder.createAndBindUi(this));
		
		// Bind the filter into the grid's store
		gridFilter.bind(grid.getStore());
		
		date.setPropertyEditor(UI.datePropertyEditor());
		salesInvoice.setHideTrigger(true);
		
		// Initialize loaders
		RpcProxy<PagingLoadConfig, PagingLoadResult<DebitMemo>> proxy = new RpcProxy<PagingLoadConfig, PagingLoadResult<DebitMemo>>() {
		    @Override
		    public void load(PagingLoadConfig loadConfig, AsyncCallback<PagingLoadResult<DebitMemo>> callback) {
		    	TransactionLoadConfig tConfig = new TransactionLoadConfig(loadConfig);
		    	setFilterInfo(tConfig);
		    	SalesServices.debitMemoSvc().getList(tConfig, callback);
		    }
	    };
	    
	    loader = new PagingLoader<PagingLoadConfig, PagingLoadResult<DebitMemo>>(proxy);
	    loader.addLoadHandler(new LoadResultListStoreBinding<PagingLoadConfig, DebitMemo, PagingLoadResult<DebitMemo>>(grid.getStore()) {
        	@Override
        	public void onLoad(LoadEvent<PagingLoadConfig, PagingLoadResult<DebitMemo>> event) {
        		super.onLoad(event);
        		grid.getSelectionModel().deselectAll();
        		edit(null);
        	}
        });
        
        grid.setLoader(loader);
        grid.setLoadMask(true);
		
		pagingToolbar.bind(loader);
		
		// Initialize the driver, used to run the form editor
		driver.initialize(this);
		
		debitMemoNo.addValidator(new MaxLengthValidator(FieldSpecs.DM_NO_MAX));
		remarks.addValidator(new MaxLengthValidator(FieldSpecs.REMARKS_MAX));
	}
		
	// UiFactory Methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	@Ignore
	@UiFactory
	StoreFilterField<DebitMemo> initializeGridFilter() {
		StoreFilterField<DebitMemo> f = new StoreFilterField<DebitMemo>() {
			@Override
			protected boolean doSelect(Store<DebitMemo> store, DebitMemo parent, DebitMemo item, String filter) {
				String filterInLowerCase = filter.toLowerCase().trim();
				return item.getDebitMemoNo().toLowerCase().contains(filterInLowerCase);
			}
		};
		return f;
	}
	
	/**
	 * Used to create the 'grid' field in the XML file. 
	 * 
	 * @return
	 */
	@UiFactory
	Grid<DebitMemo> initializeGrid() {
		DebitMemoProperties props = DebitMemoProperties.INSTANCE;
		
		List<ColumnConfig<DebitMemo, ?>> columns = new ArrayList<ColumnConfig<DebitMemo, ?>>();
		ListStore<DebitMemo> store = new ListStore<DebitMemo>(props.key());

		columns.add(UI.column(props.date(), 50, "Date", UI.dateCell()));
		columns.add(UI.column(props.companyCode(), 50, "Company"));
		columns.add(UI.column(props.debitMemoNo(), 70, "D/M No.", new TransactionNoCell<DebitMemo>(store)));
		columns.add(UI.column(props.type(), 70, "D/M Type"));
		ColumnConfig<DebitMemo, String> column = UI.column(props.customerCode(), 100, "Customer");
		columns.add(column);
		columns.add(UI.column(props.salesInvoiceNo(), 70, "Invoice No."));
		columns.add(UI.column(props.amount(), 80, "Amount", UI.decimalCell(), HasHorizontalAlignment.ALIGN_RIGHT));
//		columns.add(UI.column(props.status(), 60, "Status", TransactionStatusCell.INSTANCE, HasHorizontalAlignment.ALIGN_CENTER));
		
		ColumnModel<DebitMemo> cm = new ColumnModel<DebitMemo>(columns);

		final Grid<DebitMemo> grid = new Grid<DebitMemo>(store, cm);
		grid.setAllowTextSelection(false);
		grid.getSelectionModel().setSelectionMode(SelectionMode.MULTI);
		
		GridView<DebitMemo> view = new BasicGridView<DebitMemo>();
		view.setEmptyText("There are no debit memos to display.");
		view.setAutoExpandColumn(column);
		grid.setView(view);
		
		// Add a listener to the grid
		grid.getSelectionModel().addSelectionChangedHandler(new SelectionChangedHandler<DebitMemo>() {
			@Override
			public void onSelectionChanged(SelectionChangedEvent<DebitMemo> event) {
				onGridSelectionChanged(event);
			}
		});
		
		return grid;
	}
	
	@Ignore
	@UiFactory
	SimpleComboBox<DebitMemoType> initializeDebitMemoTypeCombo() {
		return new EnumComboBox<DebitMemoType>(DebitMemoType.values());
	}
	
	// Logic %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	void onGridSelectionChanged(SelectionChangedEvent<DebitMemo> event) {
		final DebitMemo debitMemo = grid.getSelectionModel().getSelectedItem();
		if (debitMemo == null)
			return;
		
		if (driver.isDirty()) { // If there are unsaved changes in the form
			UI.confirm("Unsaved Changes", "There are unsaved changes. Would you like to continue loading?",
				new ConfirmDialogHandler() {
					@Override
					public void onYes(HideEvent event) {
						edit(debitMemo);
					}
					
					public void onCancel(HideEvent event) {
						// just close the message box
					}
					
					public void onNo(HideEvent event) {
						// previously selected item should remain selected 
					}
				}
			);
		} else {
			edit(debitMemo);
		}
	}
	
	/**
	 * Loads <tt>debitMemo</tt> into the form editor.
	 * 
	 * @param debitMemo
	 */
	private void edit(final DebitMemo debitMemo) {
		driver.edit(debitMemo);
		
		saveBtn.setEnabled(debitMemo != null);
		cancelBtn.setEnabled(debitMemo != null);
	}
	
	// Listeners %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	@UiHandler("statusMenu")
	void onStatusSelection(SelectionEvent<Item> e) {
		loadData();
	}
	
	// Called when the 'Add' button is clicked
	@UiHandler("addBtn")
	void addClicked(SelectEvent e) {
		Window w = (Window) new CreateDebitMemoWindow().asWidget();
		w.show();
	}
	
	// Called when the 'Post' button is clicked
	@UiHandler("postBtn")
	void postClicked(SelectEvent e) {
		List<DebitMemo> selected = grid.getSelectionModel().getSelectedItems();
		if (selected.isEmpty()) {
			Notifications.info("Please select at least one debit memo.");
			return;
		}
		
		mainPanel.mask("Posting debit memos...");
		SalesServices.debitMemoSvc().postDebitMemos(selected, new Callback<List<DebitMemo>>(mainPanel) {
			@Override
			protected void handleSuccess(List<DebitMemo> result) {
				for (DebitMemo memo : result)
					grid.getStore().update(memo);
			}
		});
	}
	
	@UiHandler("moreBtnMenu")
	void onMenuSelection(SelectionEvent<Item> e) {
		Item item = e.getSelectedItem();
		if (item == refreshMenuItem) {
			refreshClicked();
		} else if (item == deleteMenuItem) {
			deleteClicked();
		} 
	}
	
	// Called when the 'Refresh' button is clicked
	void refreshClicked() {
		loadData();
	}
	
	// Called when the 'Delete' button is clicked
	void deleteClicked() {
		final DebitMemo selected = grid.getSelectionModel().getSelectedItem();
		if (selected == null)
			return;
		
		UI.confirm("Confirm Delete", "Are you sure you want to delete '" + selected.getDebitMemoNo() + "'?",
			new ConfirmDialogHandler() {
				@Override
				public void onYes(HideEvent event) {
					UI.mask(mainPanel, "Deleting " + selected.getDebitMemoNo() + "...");
					debitMemoSvc().delete(selected, new Callback<DebitMemo>(mainPanel) {
						@Override
						protected void handleSuccess(DebitMemo result) {
							grid.getStore().remove(result);
							
							// Clear the form editor if necessary
							DebitMemo inEditor = driver.flush();
							if (inEditor != null && inEditor.getDebitMemoNo().equals(result.getDebitMemoNo()))
								edit(null);
						}
					});
				}
			}
		);
	}
	
	// Called when the 'Save' button is clicked
	@UiHandler("saveBtn")
	void saveClicked(SelectEvent e) {
		DebitMemo memo = driver.flush();
		if (driver.hasErrors())
			return;
		
		String message = DataUtils.checkDebitMemo(memo);
		if (message != null) {
			Notifications.info(message);
			return;
		}
		
		UI.mask(mainPanel, "Updating debit memo...");
		debitMemoSvc().update(memo, new Callback<DebitMemo>(mainPanel) {
			@Override
			protected void handleSuccess(DebitMemo result) {
				grid.getStore().update(result);
				edit(result);
			}
		});
	}
	
	// Called when the 'Cancel' button is clicked
	@UiHandler("cancelBtn")
	void cancelClicked(SelectEvent e) {
		edit(null);
	}
	 
	// ITabItemPanel Methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	@Override
	public String getPanelId() {
		return SalesTasks.DEBIT_MEMOS.getId();
	}
	
	@Override
	public TabItemConfig getConfig() {
		if (config == null) {
			config = new TabItemConfig("Debit Memos", true);
			config.setIcon(SalesResources.INSTANCE.debitMemos());
		}
		return config;
	}
	
	@Override
	public void loadData() {
		Log.debug("Loading companies, customers, products for dropdown...");

		UI.mask(company, "Loading company...");
		companySvc().getNameAndIdOnly(new Callback<List<Company>>(company) {
			@Override
			protected void handleSuccess(List<Company> result) {
				company.getStore().clear();
				company.getStore().addAll(result);
			}
		});
		
		UI.mask(customer, "Loading customers...");
		customerSvc().getCustomerNameAndIdOnly(new Callback<List<Customer>>(customer) {
			@Override
			protected void handleSuccess(List<Customer> result) {
				customer.getStore().clear();
				customer.getStore().addAll(result);
			}
		});
		
		// Load data
		Log.debug("Loading Debit Memos...");
		loader.load();
		edit(null);
	}
	
	// Helper Methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	void setFilterInfo(TransactionLoadConfig config) {
		// Status
		if (pendingOnlyMenuItem.isChecked())
			config.setStatuses(TransactionStatus.PENDING);
		else if (postedOnlyMenuItem.isChecked())
			config.setStatuses(TransactionStatus.POSTED);
		else
			config.setStatuses((List<TransactionStatus>) null);
	}
	
}
